/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package config;

import java.sql.*;

/**
 *
 * @author CuongVM_B01135
 */
public class MyConnection {

    // private static String url;
    private static String server;
    private static String port;
    private static String database;
    private static String user;
    private static String pass;
    private static Connection conn;

    public MyConnection() {
    }

    public static Connection getConn() {
        return conn;
    }

    public static void setConn(Connection conn) {
        MyConnection.conn = conn;
    }

    public static String getDatabase() {
        return database;
    }

    public static void setDatabase(String database) {
        MyConnection.database = database;
    }

    public static String getPass() {
        return pass;
    }

    public static void setPass(String pass) {
        MyConnection.pass = pass;
    }

    public static String getPort() {
        return port;
    }

    public static void setPort(String port) {
        MyConnection.port = port;
    }

    public static String getServer() {
        return server;
    }

    public static void setServer(String server) {
        MyConnection.server = server;
    }

    public static String getUser() {
        return user;
    }

    public static void setUser(String user) {
        MyConnection.user = user;
    }

    //get method connect MSSQL
    public static boolean connect(String server, String port, String database, String user, String pass) {
        MyConnection.server = server;
        MyConnection.port = port;
        MyConnection.database = database;
        MyConnection.user = user;
        MyConnection.pass = pass;

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(getConnect());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    //get link connect
    public static String getConnect() {
        StringBuilder sb = new StringBuilder();
        sb.append("jdbc:sqlserver://");
        sb.append(server);
        sb.append(":");
        sb.append(port);

        sb.append(";databaseName=");
        sb.append(database);

        sb.append(";user=");
        sb.append(user);

        sb.append(";password=");
        sb.append(pass);

        return sb.toString();
    }

    //get method querry mssql
    public static Connection query() {
        return conn;
    }

    //get method ResultSet
    public static ResultSet resultSet(String sql) {
        ResultSet rs = null;
        try {
            PreparedStatement statement = query().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = statement.executeQuery();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return rs;
    }

    //get method update
    public static void update(String sql) {
        try {
            Statement statement = query().createStatement();
            statement.executeUpdate(sql);
        } catch (Exception e) {
        }
    }
    public static ResultSet resultStatement(String sql) {
        ResultSet rs = null;
        try {
            Statement statement = query().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = statement.executeQuery(sql);
        } catch (Exception e) {
            System.out.println(e.getMessage());     
        }
        return rs;
    }
}
